#include "ListNode.h"

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     *
     * 
     * @param pHead ListNode类 
     * @param k int整型 
     * @return ListNode类
     */
    ListNode* FindKthToTail(ListNode* pHead, int k) {
        if (pHead == nullptr || k <= 0) {
            return nullptr;
        }
        int size = length(pHead);
        int fast = size - k;
        if (fast < 0) {
            return nullptr;
        }
        if (fast == 0) {
            return pHead;
        }
        for (int i = 0; i < fast; i++) {
            pHead = pHead->next;
        }
        return pHead;
    }

    int length(ListNode* pHead) {
        int len = 0;
        while (pHead) {
            len++;
            pHead = pHead->next;
        }
        return len;
    }
};